package com.leoman.resource.service.impl;

import com.leoman.adsindex.dao.AdsIndexDao;
import com.leoman.adsindex.entity.AdsIndex;
import com.leoman.banner.dao.BannerDao;
import com.leoman.banner.entity.Banner;
import com.leoman.category.dao.CategoryDao;
import com.leoman.category.entity.Category;
import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.entity.Constant;
import com.leoman.entity.FileBo;
import com.leoman.floatwin.dao.FloatWinDao;
import com.leoman.floatwin.entity.FloatWin;
import com.leoman.resource.service.MusicService;
import com.leoman.resources.dao.MusicResourceDao;
import com.leoman.resources.entity.MusicResource;
import com.leoman.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 蔡琦 on 2016/6/28 0028.
 *
 */
@Service
public class MusicServiceImpl extends GenericManagerImpl<MusicResource, MusicResourceDao> implements MusicService {

    @Autowired
    private MusicResourceDao musicResourceDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BannerDao bannerDao;

    @Autowired
    private AdsIndexDao adsIndexDao;

    @Autowired
    private FloatWinDao floatWinDao;

    @Override
    public Page<MusicResource> page(Integer pageNum, Integer pageSize) {
        return musicResourceDao.findAll(new PageRequest(pageNum - 1,pageSize, Sort.Direction.DESC,"id"));
    }

    @Override
    @Transactional
    public Integer deleteMusic(Long musicId) {

        // 是否链接到广告banner
        List<Banner> bannerList = bannerDao.findMusicAndGame(Constant.POSITION_004, Constant.RESOURCE_TYPE_002, musicId);
        if (bannerList.size() != 0) {
            return 2;
        }

        // 是否链接到首页推荐
        List<AdsIndex> adsIndexList = adsIndexDao.findMusicAndGame(Constant.POSITION_004, Constant.RESOURCE_TYPE_002, musicId);
        if (adsIndexList.size() != 0) {
            return 2;
        }

        // 是否链接到浮窗广告
        List<FloatWin> floatWinList = floatWinDao.findMusicAndGame(Constant.POSITION_004, Constant.RESOURCE_TYPE_002, musicId);
        if (floatWinList.size() != 0) {
            return 2;
        }

        MusicResource musicResource = musicResourceDao.findOne(musicId);
        delete(musicResource);
        return 1;

        // 对应分类的数量减1
        /*Category category = musicResource.getCategory();
        if (category != null) {
            category.setCount(category.getCount() - 1);
            categoryDao.save(category);
        }*/
    }

    @Override
    @Transactional
    public Integer saveMusic(Long categoryId,
                             String name,
                             String singer,
                             @RequestParam(value = "imageFile", required = false) MultipartFile file) throws IOException {
        MusicResource musicResource = new MusicResource();
        Category category = categoryDao.findOne(categoryId);
        /*category.setCount(category.getCount() + 1);
        categoryDao.save(category);*/
        musicResource.setCategory(category);
        musicResource.setName(name);
        musicResource.setSinger(singer);

        if (file.getSize() > 10 * 1024 * 1024) {
            return 3;
        }

        //MultipartFile multipartFile = file;

        // 验证音乐格式
        String originalFileName = file.getOriginalFilename().toLowerCase();
        String fileType = originalFileName.substring(originalFileName.indexOf("."));

        List<String> list = new ArrayList<>();
        list.add(".mp3");
        list.add(".wav");
        list.add(".ogg");

        if (!list.contains(fileType)) {
            return 2;
        }
        if (!file.equals(null)) {
            FileBo fileBo = FileUtil.save(file, false);
            musicResource.setImageUrl(fileBo.getPath());
        }

        musicResourceDao.save(musicResource);
        return 1;
    }

    @Override
    public List<MusicResource> findByCategoryId(Long categoryId) {
        return musicResourceDao.iFindListByCategoryId(categoryId);
    }
}

